# -*- coding: UTF-8 -*-
# 自定义异常错误的中间件

import logging

from django.shortcuts import render_to_response


class ExceptionMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    # 如果注册多个process_exception函数，那么函数的执行顺序与注册的顺序相反。(其他中间件函数与注册顺序一致)
    # 中间件函数，用到哪个就写哪个，不需要写所有的中间件函数。
    def process_exception(self, request, exception):
        logging.debug("22222222222222222222222222222222")
        print('----process_exception1----')
        print(exception)
        print(type(exception))
        import traceback
        logging.debug(traceback.format_exc())

        return render_to_response("error/404.html")


# 日志拦截器
class LogMiddleware:

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_request(self, request):
        print('----process_request----')
        logging.debug('----process_request----')
        return None

    def process_response(self, request, response):
        print('----process_response----')
        logging.debug('----process_response----')
        return response
